// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/common/css/reset.css'
import App from './App'
import router from './router'
import store from '@/store'
import { getLogin } from '@/utils/login'
import createRoutes,{ LayoutIndex } from '@/utils/createRoutes'


Vue.config.productionTip = false

Vue.use(ElementUI)

router.beforeEach((to, from, next) => {
  // console.log(from)
  // console.log(to)
  const loginInfo = getLogin();
  const postToken = loginInfo?loginInfo.token: '';
  if(from.path === '/' && store.state.menu.menuList.length === 0 && to.path !== '/' && to.path !== '/login'){
    store.dispatch('menu/getMenuAction').then(()=>{
      const childRoutes = createRoutes(store.state.menu.menuList)
      const routes = [{
        path: '/back',
        name: 'LayoutIndex',
        component: LayoutIndex,
        children:[]
      }]
      childRoutes.map(item=>{
        routes[0].children.push(item)
      })
      console.log(routes)
      router.addRoutes(routes)
    })


  }
  if(to.path !== '/' && to.path !== '/login' && postToken === ''){
    next({path:'/'})
  }
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
